//
//  HNSH_MineViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/27.
//

#define KTopViewHeight      kStatusBarHeight+154

#import "HNSH_MineViewController.h"

//view
#import "HNSH_MineViewOrderCell.h"
#import "HNSH_MineViewOtherCell.h"
#import "HNSH_HomeScrollCell.h"

#import "HNSH_MineViewOtherHeader.h"

//#import "HNSH_RewardToors.h"
#import "HNSH_UploadLocalAlert.h"

//vc
#import "HNSH_NoticeViewController.h"                   //官方通知
//#import "HNSH_RealNameAuthViewController.h"             //实名认证
#import "HNSH_InviteFriendsViewController.h"            //邀请好友
#import "HNSH_MyTeamViewController.h"                   //我的团队
#import "HNSH_AgentViewController.h"                    //我的代理
#import "HNSH_RewardPoolManager.h"                      //奖励池
#import "HNSH_AddressViewController.h"                  //收货地址
#import "HNSH_FavoriteListViewController.h"             //收藏
#import "HNSH_ShopApplyViewController.h"                //店铺入驻
#import "HNSH_SignViewController.h"                     //签到
#import "HNSH_LevelViewController.h"                    //交易等级
#import "HNSH_AccountViewController.h"                  //账户信息
#import "HNSH_AccountEditViewController.h"              //个人信息
#import "HNSH_GoodsLevelViewController.h"               //商品等级
#import "HNSH_EcologyManager.h"                         //生态应用

#import "HNSH_RefundApplyListViewController.h"          //售后
#import "HNSH_OrderListManager.h"                       //订单列表
#import "HNSH_PurseViewController.h"                    //钱包
#import "HNSH_CustomerToors.h"                          //我的客服
#import "HNSH_ChatHomeViewController.h"                 //聊天

#import "HNSH_TaskCenterManage.h"

@interface HNSH_MineViewController ()

/* topView */
@property (nonatomic, strong) HNSH_MineViewOtherHeader *topView;

/* bottomCellItemAry */
@property (nonatomic, strong) NSArray *bottomCellItemAry;

@end

static NSString *HNSH_MineViewOrderCellID = @"HNSH_MineViewOrderCellID";
static NSString *HNSH_MineViewOtherCellID = @"HNSH_MineViewOtherCellID";
static NSString *HNSH_HomeScrollCellID = @"HNSH_HomeScrollCellID";

static NSString *HNSH_MineViewOtherHeaderID = @"HNSH_MineViewOtherHeaderID";
static NSString *HNSH_MineViewHeaderID = @"HNSH_MineViewHeaderID";

@implementation HNSH_MineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([ST_UserManager alreadyLogin]) {
        //防止重复请求失败
        self.topView.userInfo = [ST_UserManager shareInstance].user;
        [self.collectionView reloadData];
        [[ST_UserManager shareInstance] loadUserInfoComplate:^{
            self.topView.userInfo = [ST_UserManager shareInstance].user;
            [self.collectionView reloadData];
        }];
    } else {
        self.topView.userInfo = nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configTopView];
    [self configCollectionView];
}

- (void)configTopView {
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 284)];
    imgV.image = [UIImage imageNamed:@"mine_top_bg"];
    [self.view addSubview:imgV];
    [self.view sendSubviewToBack:imgV];
    
//    [self.view addSubview:self.topView];
}

- (void)configCollectionView {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KUserShowHiddenContentID]) {
        self.bottomCellItemAry = @[
            @{@"title":@"官方公告",@"image":@"mine_bottom_cell_0"},
            @{@"title":@"实名认证",@"image":@"mine_bottom_cell_1"},
            @{@"title":@"我的团队",@"image":@"mine_bottom_cell_2"},
            @{@"title":@"我的收藏",@"image":@"mine_bottom_cell_3"},
            @{@"title":@"交易等级",@"image":@"mine_bottom_cell_4"},
            @{@"title":@"区域代理",@"image":@"mine_bottom_cell_5"},
            @{@"title":@"地址管理",@"image":@"mine_bottom_cell_6"},
            @{@"title":@"账户设置",@"image":@"mine_bottom_cell_7"}
        ];
    } else {
        self.bottomCellItemAry = @[
            @{@"title":@"官方公告",@"image":@"mine_bottom_cell_0"},
            @{@"title":@"实名认证",@"image":@"mine_bottom_cell_1"},
            @{@"title":@"我的收藏",@"image":@"mine_bottom_cell_3"},
            @{@"title":@"地址管理",@"image":@"mine_bottom_cell_6"},
            @{@"title":@"账户设置",@"image":@"mine_bottom_cell_7"},
        ];
    }
    
    
    self.view.backgroundColor = [UIColor appBGColor];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.collectionView registerClass:[HNSH_MineViewOrderCell class] forCellWithReuseIdentifier:HNSH_MineViewOrderCellID];
    [self.collectionView registerClass:[HNSH_MineViewOtherCell class] forCellWithReuseIdentifier:HNSH_MineViewOtherCellID];
    [self.collectionView registerClass:[HNSH_HomeScrollCell class] forCellWithReuseIdentifier:HNSH_HomeScrollCellID];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HNSH_MineViewHeaderID];
    [self.collectionView registerClass:[HNSH_MineViewOtherHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HNSH_MineViewOtherHeaderID];
}

#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
//设置海报图片
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HNSH_MineViewOrderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_MineViewOrderCellID forIndexPath:indexPath];
        WeakSelf(weakSelf);
        cell.orderBlock = ^(NSInteger index) {
            if (![ST_UserManager alreadyLogin]) {
                [ST_UserManager gotoLogin];
                return;
            }
            if (index == 5) {
                HNSH_RefundApplyListViewController *vc = [[HNSH_RefundApplyListViewController alloc]init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
                return;
            }
            HNSH_OrderListManager *vc = [[HNSH_OrderListManager alloc]init];
            vc.index = index;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        return cell;
    } else if (indexPath.section == 1) {
        HNSH_HomeScrollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_HomeScrollCellID forIndexPath:indexPath];
        return cell;
    } else if (indexPath.section == 2) {
        HNSH_MineViewOtherCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_MineViewOtherCellID forIndexPath:indexPath];
        cell.fwAry = self.bottomCellItemAry;
        WeakSelf(weakSelf);
        cell.otherBlock = ^(NSDictionary * _Nonnull dict) {
            [weakSelf configBottomCellWithDict:dict];
        };
        return cell;
    }
    UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(UICollectionViewCell.class) forIndexPath:indexPath];
    
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
            HNSH_MineViewOtherHeader * header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HNSH_MineViewOtherHeaderID forIndexPath:indexPath];
            self.topView = header;
            return header;
        } else {
            UICollectionReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HNSH_MineViewHeaderID forIndexPath:indexPath];
            header.backgroundColor = [UIColor clearColor];
            return header;
        }
    }
    UICollectionReusableView *reusableView = [collectionView supplementaryViewForElementKind:NSStringFromClass(UICollectionReusableView.class) atIndexPath:indexPath];
    return reusableView;
}
#pragma mark UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenWidth, 123);
    } else if (indexPath.section == 1) {
        return CGSizeMake(kScreenWidth, 80);
    } else if (indexPath.section == 2) {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:KUserShowHiddenContentID]) {
            return CGSizeMake(kScreenWidth, self.bottomCellItemAry.count*40+10);
        } else {
            return CGSizeMake(kScreenWidth, self.bottomCellItemAry.count*40+10);
        }
    }
    return CGSizeZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:KUserShowHiddenContentID]) {
            return CGSizeMake(kScreenWidth, kStatusBarHeight+227);
        }
        return CGSizeMake(kScreenWidth, kStatusBarHeight+100);
    }
    return CGSizeMake(kScreenWidth, 12);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}
#pragma mark UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (![ST_UserManager alreadyLogin]) {
        [ST_UserManager gotoLogin];
        return;
    }
//    if (indexPath.section == 1) {
//        HNSH_SignViewController *vc = [[HNSH_SignViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
//    }
//    
//    if (indexPath.section == 2) {
//        NSDictionary *dict = self.bottomCellItemAry[indexPath.row];
//        [self configBottomCellWithDict:dict];
//    }
}

- (void)configBottomCellWithDict:(NSDictionary *)dict {
//    HNSH_TaskCenterManage *vc = [[HNSH_TaskCenterManage alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
//    return;
    NSString *title = [NSString stringWithFormat:@"%@",dict[@"title"]];
    if ([title isEqualToString:@"官方公告"]) {//官方公告
        HNSH_NoticeViewController *vc = [[HNSH_NoticeViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"实名认证"]) {//实名认证
        if (![[ST_UserManager shareInstance].user.location isNotBlank] && [ST_UserManager alreadyLogin]) {
            HNSH_UploadLocalAlert *alert = [[HNSH_UploadLocalAlert alloc]initWithFrame:CGRectMake(0, 0, 300, 204)];
            [alert showView];
            return;
        }
//        HNSH_RealNameAuthViewController *vc = [[HNSH_RealNameAuthViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"邀请好友"]) {//邀请好友
        HNSH_InviteFriendsViewController *vc = [[HNSH_InviteFriendsViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"我的团队"]) {//我的团队
        HNSH_MyTeamViewController *vc = [[HNSH_MyTeamViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"我的代理"]) {//我的代理
        HNSH_AgentViewController *vc = [[HNSH_AgentViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"地址管理"]) {//收货地址
        HNSH_AddressViewController *vc = [[HNSH_AddressViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"我的收藏"]) {//我的收藏
        HNSH_FavoriteListViewController *vc = [[HNSH_FavoriteListViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"店铺入驻"]) {//店铺入驻
        return;
        HNSH_ShopApplyViewController *vc = [[HNSH_ShopApplyViewController alloc]init];
        vc.type = @"20";
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"商家入驻"]) {//商家入驻
        HNSH_ShopApplyViewController *vc = [[HNSH_ShopApplyViewController alloc]init];
        vc.type = @"20";
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"签      到"]) {//签      到
        HNSH_SignViewController *vc = [[HNSH_SignViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"账户设置"]) {//账户设置
        HNSH_AccountViewController *vc = [[HNSH_AccountViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"交易等级"]) {//交易等级
        HNSH_LevelViewController *vc = [[HNSH_LevelViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"用户等级"]) {//用户等级
        HNSH_GoodsLevelViewController *vc = [[HNSH_GoodsLevelViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"悦享聊天"]) {//悦享聊天
        HNSH_ChatHomeViewController *vc = [[HNSH_ChatHomeViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"我的钱包"]) {//我的钱包
        HNSH_PurseViewController *vc = [[HNSH_PurseViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"生态应用"]) {//生态应用
        HNSH_EcologyManager *vc = [[HNSH_EcologyManager alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([title isEqualToString:@"我的客服"]) {//我的客服
        [[HNSH_CustomerToors share] showCustomer];
    } else {
        [self.view makeToast:@"暂未开放"];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
